package com.maiworld.seller.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.common.status.Status;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.maiworld.api.dto.StudentDTO;
import com.maiworld.api.vo.ResultVO;
import com.maiworld.mapper.TbNewtableMapper;
import com.maiworld.mapper.TbOrderMapper;
import com.maiworld.mapper.TbStudentMapper;
import com.maiworld.pojo.TbNewtable;
import com.maiworld.pojo.TbOrder;
import com.maiworld.pojo.TbStudent;
import com.maiworld.pojo.TbStudentExample;
import com.maiworld.pojo.TbStudentExample.Criteria;

import com.maiworld.seller.service.StudentService;

import entity.PageResult;

@Service
public class StudentServiceImpl implements StudentService {

	@Autowired
	private TbStudentMapper tbStudentMapper;
	
	@Autowired
	private TbNewtableMapper  tbNewtableMapper ;
	
	@Autowired
	private TbOrderMapper tbOrderMapper;
	
	@Override
	public List<TbStudent> findAll() {
		
		return tbStudentMapper.selectByExample(null);
	}

	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		Page<TbStudent> page = (Page<TbStudent>) tbStudentMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public void add(TbStudent student) {
		student.setCreated(new Date());
		tbStudentMapper.insert(student);

	}

	@Override
	public void update(TbStudent student) {
		tbStudentMapper.updateByPrimaryKey(student);

	}

	@Override
	public TbStudent findOne(Long sid) {
		
		return tbStudentMapper.selectByPrimaryKey(sid);
	}

	@Override
	public void delete(Long[] sids) {
		for(Long sid:sids){
			tbStudentMapper.deleteByPrimaryKey(sid);
		}

	}

	@Override
	public PageResult findPageStudent(TbStudent student, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		TbStudentExample example = new TbStudentExample();		
		Criteria create = example.createCriteria();
			if(student!= null){
				if(student.getName()!=null && student.getName().length()>0){
					create.andNameLike("%"+student.getName()+"%");
				}if(student.getStatus()!=null && student.getStatus().length()>0){
					create.andStatusEqualTo(student.getStatus());
				}if(student.getStudyStatus()!=null && student.getStudyStatus().length()>0){
					create.andStudyStatusLike("%"+student.getStudyStatus()+"%");
				}if(student.getPhone()!=null && student.getPhone().length()>0){
					create.andPhoneLike("%"+student.getPhone()+"%");
				}
			}
			Page<TbStudent> page = (Page<TbStudent>) tbStudentMapper.selectByExample(example);
			return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public ResultVO<StudentDTO> findOne(String openid) {
		ResultVO<StudentDTO> result = new ResultVO<>();
		StudentDTO studentDTO = new StudentDTO();
		if(openid==null && openid==""){
			result.setCode(0);
			result.setMsg("没有此学员的信息");
			result.setData(null);
			return result;
		}
		TbStudent selectByOpenid = tbStudentMapper.selectByOpenid(openid);
		if(selectByOpenid==null){
			result.setCode(0);
			result.setMsg("没有此学员的信息");
			result.setData(null);
			return result;
		}
		studentDTO.setTbStudent(selectByOpenid);
		result.setCode(1);
		result.setMsg("成功获取学员的信息");
		result.setData(studentDTO);
		return result;
	}
	//上传驾驶证照片
	@Override
	public Map<String, Object> addStudentUrl(Map<String, String> params) {
		Map<String, Object> result = new HashMap<String,Object>();
		TbStudent student = new TbStudent();
		student.setSid(Long.parseLong(params.get("sid")));
		student.setStatus(params.get("status"));
		student.setOpenid(params.get("openid"));
		student.setImgurl1(params.get("imgurl1"));
		student.setImgurl2(params.get("imgurl2"));		
		tbStudentMapper.updateByPrimaryKeySelective(student);		
		result.put("statusCode", 200);
		result.put("message", "上传成功");		
		return result;
	}
	
	//获取驾照图片
	public Map<String, Object> findOneSid(Long sid) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		
		List<String> array = new ArrayList<String>();
		TbStudent stu = new TbStudent();		
		stu = tbStudentMapper.selectByPrimaryKey(sid);		
		array.add(stu.getImgurl1());
		array.add(stu.getImgurl2());
		if(array.isEmpty()){
			result.put("drive", null);
		}else {
			result.put("drive", array);
		}
		result.put("statusCode", 200);
		result.put("message", "成功");		
		return result;
	}

	//修改用户信息
	@Override
	public Map<String, Object> updateStudent(Map<String, String> params) {
		
		Map<String, Object> result = new HashMap<String,Object>();
		TbStudent student = new TbStudent();
		student.setSid(Long.parseLong(params.get("sid")));
		
		student.setName(params.get("name"));
		student.setSex(params.get("sex"));
		student.setCardId(params.get("cardId"));
		student.setPhone(params.get("phone"));		
		student.setHeadPic(params.get("headPic"));
		student.setOpenid(params.get("openid"));
		student.setStatus(params.get("status"));
		tbStudentMapper.updateByPrimaryKeySelective(student);		
		result.put("statusCode", 200);
		result.put("message", "修改成功");		
		return result;
	}
	//用户登陆
	public Map<String, Object> login(String openid,String nickname, String head_pic){
		Map<String, Object> result = new HashMap<String,Object>();
		TbStudent stu = new TbStudent();
		stu.setOpenid(openid);
		
		List<TbStudent> a = tbStudentMapper.selectByStudent(stu);
		if(a.size() > 0){
			//存在用户
			result.put("userinfo", a.get(0));
			result.put("openid", openid);
			result.put("statusCode", 200);
			result.put("message", "登录成功");	
		}else{
			stu.setHeadPic(head_pic);
			stu.setNickName(nickname);
			stu.setCreated(new Date());
			int res = tbStudentMapper.insertSelective(stu);
			if(res > 0){
				List<TbStudent> b = tbStudentMapper.selectByStudent(stu);
				result.put("userinfo", b.get(0));
				result.put("openid", openid);
				result.put("statusCode", 200);
				result.put("message", "登录成功");	
			}else{
				result.put("statusCode", 500);
				result.put("message", "注册失败");	
			}
		}
		return result;
	}
	//学员选择驾校自己的分校
	@Override
	public Map<String, Object> updateStatus(Long sid, String openid, String status) {
		
		HashMap<String, Object> result = new HashMap<String,Object>();
		TbStudent stu = new TbStudent();
		stu.setSid(sid);
		 stu.setStatus(status);
		 tbStudentMapper.updateByPrimaryKeySelective(stu);
	 
		TbStudent a =  tbStudentMapper.selectByPrimaryKey(sid);

		result.put("userInfo", a);
		result.put("statusCode", 200);
		result.put("message", "成功");		
		return result;
	}

	//报名成功，改变状态
	@Override
	public Map<String, Object> updatOrderNews(Long order_id) {
		
		HashMap<String, Object> result = new HashMap<String,Object>();
		
		TbOrder order = tbOrderMapper.selectByPrimaryKey(order_id);
		order.setPaymentStatus("1");
		tbOrderMapper.updateByPrimaryKeySelective(order);
		
		TbNewtable news = tbNewtableMapper.selectByPrimaryKey(order.getNewsId());
		Integer a = news.getNepoints();
		
		TbStudent str = tbStudentMapper.selectByPrimaryKey(order.getSid());
		str.setPoints(a);
		str.setStudyStatus("1");
		str.setName(order.getUsername());
		str.setPhone(order.getPhone());
		str.setCardId(order.getCard());
		tbStudentMapper.updateByPrimaryKeySelective(str);
		result.put("statusCode", 200);
		result.put("message", "成功");
		return result;
	}
	


}
